#ifndef _CSHIP_H_
#define _CSHIP_H_

#include "CAnimatedObject.h"
#include "CBullet.h"
#include "CEnemy.h"
#include "CShield.h"
#include "CEngine.h"
#include "cocos2d.h"
#include "Box2D/Box2D.h"

/**
 * This class controls the ship's movement, animation and interaction.
 */
class CShip : public CAnimatedObject
{
public:
	CShip();
	~CShip();
	bool init(char* frameName, cocos2d::CCSpriteBatchNode* pSpriteSheet, b2World* pWorld);
	void setMove(cocos2d::CCPoint move);
	void move(float dt);
	void shoot();
	void restoreSprite();
	void doABarrelRoll();
	void collision(float damage);
	cocos2d::CCArray* getBullets();
	cocos2d::CCRect getRect();
	float getCollisionDamage();
	void checkBulletCollisions(CEnemy* pEnemy);
	float getCurrentHealthPoints();
	void cleanBullets();
	void cleanShield();
	void hit();
	void changeGunType();
	float getBulletInterval();

private:
	enum ShipDirection
	{
		Normal,
		Back,
		Forward
	};

	/**
	 * Ship movement speed.
	 */
	float								__speed;

	/**
	 * Size of the screen.
	 */
	cocos2d::CCSize						__winSize;

	/**
	 * This value specifies a move vector by which the ship should move.
	 * It's set in didAccelerate() callback in the MainLayer class.
	 *
	 */
	cocos2d::CCPoint					__move;

	/**
	 * Set in the runMoveActions().
	 */
	cocos2d::CCFiniteTimeAction*    	__pMoveAction;

	/**
	 * Set in the runMoveActions().
	 */
	cocos2d::CCFiniteTimeAction*    	__pEngineMoveAction;

	/**
	 * Set in the runMoveActions().
	 */
	cocos2d::CCFiniteTimeAction*    	__pShieldMoveAction;
	/**
	 * Handles the shield animation.
	 */
	CShield*								__pShield;

	/**
	 * Handles the engine animation.
	 */
	CEngine*								__pEngine;

	/**
	 * Updated in changeMovementAnimation().
	 */
	ShipDirection						__currentShipDirection;

	/**
	 * Restores the original image of the ship. Use by the shoot() method.
	 */
	cocos2d::CCFiniteTimeAction*		__pRestoreSpriteAction;

	/**
	 * The array of fired bullets.
	 * Updated in cleanBullets() method called by MainLayer::update() method.
	 */
	cocos2d::CCArray*	__pBullets;

	/**
	 * If the value is true, the ship can't receive damage.
	 */
	bool								__invulnerable;

	/**
	 * The initial value of ship's shield points.
	 */
	float								__shieldPointsBase;

	/**
	 * The initial value of ship's health points.
	 */
	float								__healthPointsBase;

	/**
	 * This value is modified in the collision(float damage) method.
	 */
	float								__currentShieldPoints;

	/**
	 * This value is modified in the collision(float damage) method.
	 */
	float								__currentHealthPoints;

	/**
	 * This value is modified in changeGunType() method called by the MainLayer.
	 */
	CBulletParams::EBulletType			__gunType;

	/**
	 * The parameters are set in the Ship class and are passed to the Bullet class.
	 */
	CBulletParams						__bulletParams;

	/**
	 * Defines the interval between consecutive bullet shots in standard mode.
	 */
	float								__standardGunInterval;

	/**
	 * Defines the interval between consecutive bullet shots in bomb mode.
	 */
	float								__bombGunInterval;

	bool initEngineAndShield(char* frameNamePrefix, cocos2d::CCSpriteBatchNode* pSpriteSheet, b2World* pWorld);
	void turnOffInvulnerability();
	void stopMoveActions();
	void applySpeedLimit(const int speedLimit);
	void applyDeadZone(float* moveX, float* moveY);
	void limitMovementArea(cocos2d::CCPoint* newPosition);
	void runMoveActions(float moveDuration, cocos2d::CCPoint newPosition);
	void changeMovementAnimation(float moveX);
	void createBoxBody();
};

#endif //_CSHIP_H_